$(function () {
    //验证订单编码3
    $('#billCode').blur(checkBillCode)
    //商品名称
    $('#productName').blur(checkProductName)
    //验证商品单位
    $('#productUnit').blur(checkProductUnit)
    //验证商品数量
    $('#productCount').blur(checkProductCount)
    //验证总金额
    $('#totalPrice').blur(checkTotalPrice)
    //验证供应商
    $('.providers').blur(checkProviders)
    //表单提交
    $('.updateForm').submit(function (e) {
        e.preventDefault()
        //判断当前的表单验证是否通过
        let flag = checkBillCode() & checkProductName() & checkProductUnit()
         & checkProductCount() & checkTotalPrice() & checkProviders() 
        if (flag === 0) {
            return alert('请正确填写修改信息')
        }
        //提交表单进行添加操作
        getAjax()
    })
})

//验证订单编码
function checkBillCode() {
    let billCode = $('#billCode')
    let v = billCode.val()
    let parent = billCode.parent()
    let span = billCode.next()
    span.empty()
    //判断空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('订单编码不能为空')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('订单编码正确')
    return true
}


//商品名称
function checkProductName() {
    let productName = $('#productName')
    let v = productName.val()
    let parent = productName.parent()
    let span = productName.next()
    span.empty()
    //判断空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('商品名称不能为空')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('商品名称正确')
    return true
}

//验证商品单位
function checkProductUnit() {
    let productUnit = $('#productUnit')
    let v = productUnit.val()
    let parent = productUnit.parent()
    let span = productUnit.next()
    span.empty()
    //判断空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('商品单位不能为空')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('商品单位正确')
    return true
}

//验证商品数量
function checkProductCount() {
    let productCount = $('#productCount')
    let v = productCount.val()
    let parent = productCount.parent()
    let span = productCount.next()
    span.empty()
    //判断空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('商品数量不能为空')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('商品数量正确')
    return true
}

//验证总金额
function checkTotalPrice() {
    let totalPrice = $('#totalPrice')
    let v = totalPrice.val()
    let parent = totalPrice.parent()
    let span = totalPrice.next()
    span.empty()
    //判断空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('总金额不能为空')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('总金额正确')
    return true
}

//验证供应商
function checkProviders() {
    let providers = $('.providers')
    let v = providers.val()
    let parent = providers.parent()
    let span = providers.next()
    span.empty()
    //判断空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('请选择供应商')
        return false
    }
    if (v === '1') {
        parent.removeClass('error').addClass('ok')
        span.html('您选择了北京市粮油总公司')
        return true
    }
    if (v === '2') {
        parent.removeClass('error').addClass('ok')
        span.html('您选择了邯郸市五得利面粉厂')
        return true
    }
}


async function getAjax() {
    let options = {
        url: 'http://146.56.230.35:3007/bill/createBill',
        type: 'POST',
        data: {
            billCode: $('#billCode').val(),
            productName: $('#productName').val(),
            productUnit: $('#productUnit').val(),
            productCount:parseInt( $('#productCount').val()),
            totalPrice: parseInt($('#totalPrice').val()),
            isPayment: parseInt($('.isPayment:checked').val()),
            providerId: parseInt($('.providers').val())
        }
    }
    try {
        let res = await request(options)
        console.log(res);
        if (res.code !== 200) {
            alert(res.msg)
            return null
        }
    } catch (error) {
        return null
    }

    location = 'billList.html'
}